package com.system.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.system.dao.MainMapper;
import com.system.domain.Cart;
import com.system.domain.PageBean;
import com.system.domain.Phones;
import com.system.domain.User;
import com.system.service.PhoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("phone")//这里要加上"phone"不然的话找不到PhoneServiceImpl
public class PhoneServiceImpl implements PhoneService {

    @Autowired
    private MainMapper mainMapper;


    @Override
    public List<Phones> getPhonesById() {
        return mainMapper.getPhonesById();
    }

    @Override
    public void addCart(int pid, int uid) {
        mainMapper.addCart(pid, uid);
    }

    @Override
    public List<Phones> selectCart() {
        return mainMapper.selectCart();
    }

    @Override
    public List<Phones> selectPhones() {
        return mainMapper.selectPhones();
    }

    @Override
    public int selectCartCount(Integer pid, int uid) {
        return mainMapper.selectCartCount(pid,uid);
    }

    @Override
    public int delToCart(Integer cid) {
        return mainMapper.delToCart(cid);
    }

    @Override
    public int delToCarts(String[] cids) {
        System.out.println("cids"+cids);
        return mainMapper.delToCarts(cids);

    }

    @Override
    public int selectCartCid(Integer pid, int uid) {
        return mainMapper.selectCartCid(pid,uid);
    }

    @Override
    public List<Integer> selectCartCids(String pids, int uid) {
        String[] pidTemps = pids.split(",");
//        StringBuffer stringBuffer = new StringBuffer();
//
////
////        List<Integer> pids = new ArrayList<>();
//        for (int i=0 ; i < pidTemps.length; i ++) {
//            stringBuffer.append("'"+pidTemps[i]+"'");
//            if (i != pidTemps.length -1 ) {
//                stringBuffer.append(",");
//            }
//        }
//        pids = stringBuffer.toString();
        return mainMapper.selectCartCids(pidTemps,uid);
    }

    @Override
    public int UpdateUser(User user) {
        return mainMapper.UpdateUser(user);
    }

    @Override
    public User queryUserById(int id) {
        return mainMapper.queryUserById(id);
    }

    @Override
    public List<Phones> selectPhoneByName(String name) {
        return mainMapper.selectPhoneByName(name);
    }

    @Override
    public PageBean findByPage(Cart cart, int pageCode, int pageSize) {
        System.out.println("findByPage........");
        //使用Mybaatis分页插件
        PageHelper.startPage(pageCode,pageSize);

        //调用分页查询方法,其实就是查询所有数据,mybatis自动帮助进行分页计算
        Page<Cart> page=mainMapper.findByPage(cart);
        return new PageBean(pageCode,(int)Math.ceil((double)(page.getTotal()/(double)pageSize)),(int)page.getTotal(),pageSize,page.getResult());
    }


}
